package com.yt.sys.api.client;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.yt.common.constant.Result;
import com.yt.common.enums.CodeEnum;
import com.yt.sys.models.bo.SysPermissionsSaveBO;
import com.yt.sys.models.dto.SysRolePermissionDTO;
import com.yt.sys.models.service.ISysRolePermissionService;
import com.yt.sys.models.vo.SysPermissionsVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import com.yt.sys.models.service.ISysPermissionsService;
import com.yt.sys.models.bo.SysPermissionsBO;
import com.yt.sys.models.dto.SysPermissionsDTO;

import java.util.List;

/**
 * 权限详情控制器
 *
 * @author thekai
 * @Date 2025-10-23 11:31:56
 */

@RestController
@RequestMapping("/api/sysPermissions")
@Slf4j
@Api(value = "SysPermissionsApiController",tags = "权限详情Api接口")
public class SysPermissionsApiController {

    @DubboReference
    private ISysPermissionsService sysPermissionsService;
    @DubboReference
    private ISysRolePermissionService sysRolePermissionService;

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysPermissionsDTO> get(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }

        SysPermissionsDTO dto = sysPermissionsService.selectById(id);

        return new Result<>(dto);
    }

    @ApiOperation(value = "权限详情列表",notes = "权限详情列表")
    @PostMapping("/queryPageList")
    public Result<Page<SysPermissionsDTO>> queryPageList(SysPermissionsBO bo){
        Wrapper<SysPermissionsDTO> wrapper = new EntityWrapper<>();
                if (bo.getStatus() != null){
                    wrapper.eq("status", bo.getStatus());
                }
        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<SysPermissionsDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = sysPermissionsService.selectPage(page, wrapper);

        return new Result<>(page);
    }
    @PostMapping("/addSysPermission")
    @ApiOperation(value = "增加权限信息", notes = "增加权限信息")
    public Result<Boolean> addSysPermission(@RequestBody SysPermissionsSaveBO saveBO) {
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        boolean flag = sysPermissionsService.add(saveBO);

        return new Result<>(flag);
    }
    @PostMapping("/deleteSysPermission")
    @ApiOperation(value = "删除权限信息", notes = "删除权限信息")
    @Transactional
    public Result<Boolean> deleteSysPermission(@RequestParam Long id) {
        if (id == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        //1.删除权限表
        boolean flag = sysPermissionsService.deleteById(id);
        //2.删除父权限时，删除子权限
        Wrapper<SysPermissionsDTO> wrapper1 = new EntityWrapper<>();
        wrapper1.eq("parent_id", id);
        sysPermissionsService.delete(wrapper1);

        //3.删除角色权限表
        Wrapper<SysRolePermissionDTO> wrapper = new EntityWrapper<>();
        wrapper.eq("permission_id", id);
        sysRolePermissionService.delete(wrapper);
        return new Result<>(flag);
    }
    @PostMapping("/querySysPermission")
    @ApiOperation(value = "查询权限信息", notes = "查询权限信息")
    public Result<List<SysPermissionsVO>> querySysPermission() {
        List<SysPermissionsVO> sysPermissionVOS = sysPermissionsService.query();
        return new Result<>(sysPermissionVOS);
    }

    @PostMapping("/updateSysPermission")
    @ApiOperation(value = "修改权限信息", notes = "修改权限信息")
    public Result<Boolean> updateSysPermission(@RequestBody SysPermissionsSaveBO saveBO) {
        SysPermissionsDTO sysPermissionsDTO = new SysPermissionsDTO();
        if (saveBO == null) {
            return new Result<>(CodeEnum.FAIL_100002);
        }
        BeanUtils.copyProperties(saveBO, sysPermissionsDTO);
        boolean flag = sysPermissionsService.updateById(sysPermissionsDTO);

        return new Result<>(flag);
    }
}
